package com.longxi.tianjintianbao.widget.test;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;

public class RoundRectView extends View {
    private Paint mPaintText;//文字画笔
    private Paint mPaintRectF;//圆角背景画笔
    private Paint mPaintOK;//✔画笔
    private String mWord;//显示的文字
    private RectF mRectF;//圆角矩形
    private Rect mRect;//文字矩形
    private float textSize = 50f;//文字大小


    /**
     * view的宽度
     */
    private int width;
    /**
     * view的高度
     */
    private int height;
    /**
     * 默认两圆圆心之间的距离=需要移动的距离
     */
    private int default_two_circle_distance;
    /**
     * view左右移动
     */
    private ObjectAnimator animator_move_left_right;
    /**
     * 动画执行时间
     */
    private int duration = 1000;
    /**
     * view左右移动距离
     */
    private float move_distance = 30f;
    /**
     * 是否开始绘制对勾
     */
    private boolean startDrawOk = false;

    private AnimatorSet animatorSet;//动画


    public RoundRectView(Context context) {
        super(context);
        init();
    }

    public RoundRectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;

        default_two_circle_distance = (w - h) / 2;

        initOk();
        initAnimation();
    }

    /**
     * 初始化 画笔
     */
    private void init() {
        animatorSet = new AnimatorSet();
        initPaint();
    }

    @SuppressLint("ResourceType")
    private void initPaint() {

        mPaintText = new Paint();
        mPaintText.setStrokeWidth(5);
        mPaintText.setStyle(Paint.Style.FILL);
        mPaintText.setAlpha(60);
        mPaintText.setTextSize(textSize);
        mPaintText.setAntiAlias(true);
        mPaintText.setColor(Color.WHITE);

        mPaintRectF = new Paint();
        mPaintRectF.setColor(Color.parseColor(getResources().getString(R.color.color_onpress_red)));

        mPaintOK = new Paint();
        mPaintOK.setStrokeWidth(10);
        mPaintOK.setStyle(Paint.Style.FILL);
        mPaintOK.setAntiAlias(true);
        mPaintOK.setColor(Color.WHITE);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRectF = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(mRectF,  20f, 20f, mPaintRectF);

        mRect = new Rect();
        mPaintText.getTextBounds(mWord, 0, mWord.length(), mRect);

        //判断文字宽度是否大于父类宽度 如果大于父类宽度 修改画笔TextSize大小
        if(mRect.width() >= getWidth()){
            mPaintText.setTextSize(getWidth() * textSize / mRect.width());
            mRect = new Rect();
            mPaintText.getTextBounds(mWord, 0, mWord.length(), mRect);
        }

        canvas.drawText(mWord, (getWidth() - mRect.width()) / 2, (getHeight() + mRect.height()) / 2, mPaintText);

        if (startDrawOk) {
            canvas.drawPath(path, mPaintOK);
        }
    }

    /**
     * 关闭动画
     */
    public void openAnimation() {
        if (!animatorSet.isRunning()) {
            animatorSet.play(animator_draw_ok);
//            animatorSet.play(animator_move_left_right);
            animatorSet.start();
        }
    }

    public void initAnimation(){
        btn_error_animation();
        set_draw_ok_animation();
    }

    /**
     * 按钮左右摇摆提示
     * @return
     */
    public void btn_error_animation(){
        animator_move_left_right = ObjectAnimator.ofFloat(this, "translationX", 0.0f, move_distance, -move_distance, move_distance / 2, 0.0f);
        animator_move_left_right.setDuration(1000);//动画时间
        animator_move_left_right.setInterpolator(new BounceInterpolator());//实现反复移动的效果
        animator_move_left_right.setRepeatCount(0);//设置动画重复次数
//            animator_move_left_right.setStartDelay(500);//设置动画延时执行
    }

    private PathMeasure pathMeasure;//取路径的长度
    private Path path = new Path();//路径--用来获取对勾的路径
    private PathEffect effect;//对路径处理实现绘制动画效果
    /**
     * 绘制对勾
     */
    private void initOk() {
        //对勾的路径
        path.moveTo(default_two_circle_distance + height / 8 * 3, height / 2);
        path.lineTo(default_two_circle_distance + height / 2, height / 5 * 3);
        path.lineTo(default_two_circle_distance + height / 3 * 2, height / 5 * 2);

        pathMeasure = new PathMeasure(path, true);
    }
    private ValueAnimator animator_draw_ok;//绘制对勾（√）的动画

    /**
     * 绘制对勾的动画
     */
    private void set_draw_ok_animation() {
        animator_draw_ok = ValueAnimator.ofFloat(1, 0);
        animator_draw_ok.setDuration(duration);
        animator_draw_ok.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                startDrawOk = true;
                float value = (Float) animation.getAnimatedValue();

                effect = new DashPathEffect(new float[]{pathMeasure.getLength(), pathMeasure.getLength()}, value * pathMeasure.getLength());
                mPaintOK.setPathEffect(effect);
                invalidate();
            }
        });
    }

    public String getmWord() {
        return mWord;
    }

    public void setmWord(String mWord) {
        this.mWord = mWord;
        invalidate();
    }

    public void setWordShowAnimator(String mWord){
        this.mWord = mWord;
        openAnimation();
    }
}
